
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.Sides;
import java.io.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author eros
 */
public class BasicPrintSample {



    public static boolean saveStringToFile(String filename,String stringStream){
        boolean save = false;
        BufferedWriter bw = null;
        try{
            bw = new BufferedWriter(new FileWriter(filename));
            try{
                bw.write(stringStream);
                save = true;
            }catch(IOException xcp){
                System.err.println(xcp);
            }finally{
                bw.close();
            }
        }catch(IOException xcp){
            System.err.println(xcp);
        }

        return save;
    }


    public void print() throws FileNotFoundException, PrintException{
                // Input the file

                //saveStringToFile("file.txt","eros koller da silva");
                FileInputStream textStream = new FileInputStream( new File("eros.txt"));


                // Set the document type
                DocFlavor myFormat = DocFlavor.INPUT_STREAM.TEXT_HTML_US_ASCII;//EXT_PLAIN_ASCII;
                // Create a Doc
                Doc myDoc = new SimpleDoc(textStream, myFormat, null);
                // Build a set of attributes
                PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                aset.add(new Copies(1));
                //aset.add(MediaSize.JIS.B0);
                aset.add(Sides.ONE_SIDED);
                // discover the printers that can print the format according to the
                // instructions in the attribute set
                PrintService[] services =
                        PrintServiceLookup.lookupPrintServices(myFormat, aset);
                // Create a print job from one of the print services
                if (services.length > 0) {
                        DocPrintJob job = services[0].createPrintJob();
                        try {
                                job.print(myDoc, aset);
                        } catch (PrintException pe) {}
                }

    }

    public static void main(String... args){

        BasicPrintSample.saveStringToFile("eros.txt", "eros koller da silva");

        BasicPrintSample b = new BasicPrintSample();
        try {
            b.print();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(BasicPrintSample.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PrintException ex) {
            Logger.getLogger(BasicPrintSample.class.getName()).log(Level.SEVERE, null, ex);
        }

       
    }


}
